<?php

namespace App\Http\Controllers\Home;

use App\Http\Requests\FindhouseRequest;
use App\Http\Requests\ViewHouseRequest;
use App\Services\Home\MessageService;
use iBrand\Sms\Facade as Sms;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Hashing\BcryptHasher as Hasher;
use Illuminate\Support\Facades\Hash;

class MessageController extends Controller
{
    protected $service;
    protected $hasher;

    public function __construct(MessageService $service,Hasher $hasher)
    {
        $this->service = $service;
        $this->hasher = $hasher;
    }

    public function saveFindHouseInfo(FindhouseRequest $request)
    {
        return $this->service->saveFindHouseInfo($request->all());
    }

    public function saveViewHouse(ViewHouseRequest $request)
    {
        if (!Sms::checkCode($request->input('tel'), $request->input('verify'))) {
            // 验证码验证不成功
            return response()->json([
                'code' => 1,
                'msg' => '短信动态验证码输入错误'
            ]);
        }else{
            //验证码验证成功
            $data = $request->input();
            $property = explode(',',$data['property']);
            $data['property_id'] = $property[0];
            $data['title'] = $property[1];

            return $this->service->saveViewHouseInfo($data);
        }
    }

    public function checkCaptcha(Request $request)
    {
        $res = Hash::check($request->input('captcha'),session('captcha.key'));
        if($res){
            return response()->json([
                'code' => 0
            ]);
        }else{
            return response()->json([
                'code' => 1
            ]);
        }
    }

}
